package com.pet.platform.service.impl;

import com.pet.platform.common.PageResult;
import com.pet.platform.dto.ServiceDTO;
import com.pet.platform.dto.ServiceQueryDTO;
import com.pet.platform.entity.Service;
import com.pet.platform.exception.BusinessException;
import com.pet.platform.mapper.ServiceMapper;
import com.pet.platform.service.ServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ServiceServiceImpl implements ServiceService {
    
    @Resource
    private ServiceMapper serviceMapper;
    
    @Override
    public Service findById(Long id) {
        return serviceMapper.findById(id);
    }
    
    @Override
    public PageResult<ServiceDTO> findPage(ServiceQueryDTO queryDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", queryDTO.getName());
        params.put("category", queryDTO.getCategory());
        params.put("status", queryDTO.getStatus());
        params.put("minPrice", queryDTO.getMinPrice());
        params.put("maxPrice", queryDTO.getMaxPrice());
        params.put("offset", (queryDTO.getPageNum() - 1) * queryDTO.getPageSize());
        params.put("pageSize", queryDTO.getPageSize());
        params.put("orderBy", queryDTO.getOrderBy());
        params.put("isAsc", queryDTO.getIsAsc());
        
        List<Service> services = serviceMapper.findList(params);
        Long total = serviceMapper.count(params);
        
        List<ServiceDTO> serviceDTOs = services.stream().map(this::convertToDTO)
                .collect(Collectors.toList());
        
        return new PageResult<>(serviceDTOs, total, queryDTO.getPageNum(), queryDTO.getPageSize());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(ServiceDTO serviceDTO) {
        Service service = new Service();
        BeanUtils.copyProperties(serviceDTO, service);
        serviceMapper.insert(service);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Long id, ServiceDTO serviceDTO) {
        Service existingService = serviceMapper.findById(id);
        if (existingService == null) {
            throw new BusinessException("服务不存在");
        }
        
        Service service = new Service();
        BeanUtils.copyProperties(serviceDTO, service);
        service.setId(id);
        serviceMapper.update(service);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        Service existingService = serviceMapper.findById(id);
        if (existingService == null) {
            throw new BusinessException("服务不存在");
        }
        serviceMapper.delete(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long id, Integer status) {
        Service existingService = serviceMapper.findById(id);
        if (existingService == null) {
            throw new BusinessException("服务不存在");
        }
        
        Service service = new Service();
        service.setId(id);
        service.setStatus(status);
        serviceMapper.update(service);
    }
    
    private ServiceDTO convertToDTO(Service service) {
        ServiceDTO serviceDTO = new ServiceDTO();
        BeanUtils.copyProperties(service, serviceDTO);
        return serviceDTO;
    }
} 